package com.zj.core.dialog

import android.app.Activity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.LinearLayout
import com.zj.core.R
import com.zj.core.adapter.AdapterParent
import com.zj.core.base.BaseApplication
import com.zj.core.base.BaseDialog
import com.zj.core.bean.TemplateTitleBean
import com.zj.core.interfaces.TextMessageIml
import com.zj.core.tools.ViewPosition
import com.zj.core.ui.TemplateListUi

@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
class TemplateListDialog(context: Activity?,hospitalId:String,depatmentId:String,templateList:ArrayList<TemplateTitleBean.Rows>?) :
    BaseDialog(context), View.OnClickListener {
    private var mTemplateTitleList:ArrayList<TemplateTitleBean.Rows>?=templateList
    private var mHospitalId:String=hospitalId
    private var mDepatmentId:String=depatmentId
    private var rootView: TemplateListUi?=null
    private var mTemplate: AdapterParent? = null
    private var mLayoutManager: LinearLayoutManager?=null
    private var mTemplateIml: TextMessageIml?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val width: Int = ViewPosition().getScreenWidth(this.context)
        val height: Int = (ViewPosition().getScreenHeight(this.context) * 0.95).toInt()
        val params = LinearLayout.LayoutParams(width, height, 0f)

        rootView= TemplateListUi(context, this)
        rootView?.apply {
            setContentView(common_dialog_root_view, params)

            //setCancelable(cancelable)
            setCanceledOnTouchOutside(true)

            mLayoutManager = LinearLayoutManager(BaseApplication.context)
            mLayoutManager?.orientation = LinearLayoutManager.VERTICAL

            mTemplate =AdapterParent(mTemplateTitleList,BaseApplication.context!!, mHospitalId, mDepatmentId)
            template_list_rv?.setItemViewCacheSize(1000000000)
            template_list_rv?.layoutManager = mLayoutManager
            template_list_rv?.adapter = mTemplate
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.common_dialog_cancel_tv -> dismiss()

            R.id.common_dialog_confirm_tv ->{
                mTemplateIml?.messageResult(mTemplate?.getListViewItem().toString())
                dismiss()
            }
        }
    }

    fun setTemplateListener(iml: TextMessageIml): TemplateListDialog {
        this.mTemplateIml=iml
        return this
    }
}